package com.avic.gateway.common.util;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author: huangsheng
 * @date: 2018/8/24
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    //获取上下文
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过名字获取上下文中的bean
    public static <T> T getBean(String beanName) {
        return (T) applicationContext.getBean(beanName);
    }


    //通过类型获取上下文中的bean
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}
